package com.coursemanage.schools;
import com.coursemanage.common.model.Message;
import com.coursemanage.common.model.Schools;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class SchoolsController extends Controller {

	public void index() {
		render("index.jsp");
	}
	public void page() {
		Page<Schools> courses=Schools.dao.paginate(getParaToInt("pageNumber"), 
				getParaToInt("pageSize"),getPara("keyword"),getPara("sortName"),getPara("order"));
		Ret ret=new Ret();
		ret.put("rows", courses.getList());
		ret.put("total", courses.getTotalRow());
		setAttrs(ret);
	   renderJson();
	}
	public void update(){
		Schools schools=getModel(Schools.class,"school");
		schools.update();
	    Message msg=new Message();
	    msg.setMsg(true,200,"更新成功！");
		renderJson(msg);
	}
	public void save(){
		Schools schools=getModel(Schools.class,"school");
		Message msg=new Message();
		schools.save();
	   msg.setMsg(true,200,"添加成功！");
	   renderJson(msg);
	}
	public void CreatOrEdit(){
		Integer id=getParaToInt(0);
		Schools school=id!=null&&id>0?Schools.dao.findById(id):new Schools();
		Ret ret=new Ret();
		boolean isEdit=false;
		if (school.getName()!=null) {
			isEdit=true;	
		}
		ret.put("isEdit",isEdit);
		ret.put("school",school);
		setAttrs(ret);
		render("CreatOrEdit.jsp");
	}
	public void del(){
		String[] ids=getParaValues("ids");
		
		if(ids!=null&&ids.length>-1&&ids.length==1){
			//执行删除操作
		  Db.deleteById("schools", "id", ids[0]);		
		}else if(ids.length>1){
			
			//批量删除操作
			String sql="delete from schools where id in(?)";
			Object id_no_arr[][]=new Object[ids.length][1];
			for(int i=0;i<ids.length;i++){
				for(int j=0;j<1;j++){
					id_no_arr[i][j]=ids[i];
				}
			}
			int[] size= Db.batch(sql,id_no_arr, id_no_arr.length);
		}
		Message msg = new Message();
		msg.setMsg(true, 200, "删除成功！");
		renderJson(msg);
	}
	
	
}
